x = int(input())
c = 0
if len(str(x)) == 4:
    for i in range(2, x + 1):
        if str(i) in str(x):
            x = False
            for c in range(2, i // 2 + 1):
                if i % c == 0:
                    x = True
                    break
                if c:
                    continue
                else:
                    c = max(c, i)
print(c)
                    

